package weaver.formmode.customjavacode.customsearch;

import weaver.conn.RecordSet;
import weaver.formmode.customjavacode.AbstractCustomSqlConditionJavaCode;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;

import java.util.Map;

/**
 * 1、	绩效下发、筛选福利人员名单、年终将拆分、加班费上报的权限控制
 *
 * @author Administrator
 */
public class CustomSearchQXKZ extends AbstractCustomSqlConditionJavaCode {

    /**
     * 生成SQL查询限制条件
     *
     * @param param param包含(但不限于)以下数据
     *              user 当前用户
     * @return 返回的查询限制条件的格式举例为: t1.a = '1' and t1.b = '3' and t1.c like '%22%'
     * 其中t1为表单主表表名的别名
     */

    public String generateSqlCondition(Map<String, Object> param) throws Exception {
        BaseBean baseBean = new BaseBean();
        RecordSet rs = new RecordSet();
        RecordSet recordSet = new RecordSet();

        User user = (User) param.get("user");
        int userid = user.getUID();
        baseBean.writeLog("generateSqlCondition——userid:" + userid);

        String sqlCondition = " (1=0 ";

        //数据管理员表
        String SJGLYtablename = baseBean.getPropValue("Mode_Qxkz", "SJGLYtablename");
        //区域管理员表
        String QYGLYtablename = baseBean.getPropValue("Mode_Qxkz", "QYGLYtablename");
        //数据上报员表
        String SJSBYtablename = baseBean.getPropValue("Mode_Qxkz", "SJSBYtablename");
        //系数上报新增权限表
        String STtablename = baseBean.getPropValue("Mode_Qxkz", "STtablename");
        //人力资源附加表
        String RLZYtablename = baseBean.getPropValue("WStablename", "RLZYtablename");
        //人员信息字段对应表
        String HrmFieldtablename = baseBean.getPropValue("kqReport", "HrmFieldtablename");

        boolean flag = false;
        rs.executeQuery("select ry from " + SJGLYtablename);
        while (rs.next()) {
            if (user.getUID() == Util.getIntValue(rs.getString("ry"))) {
                flag = true;
                break;
            }
        }

        if (user.getUID() == 1 || flag) {
            //系统管理员和数据管理员能看到全部数据
            sqlCondition = " 1=1 ";
            baseBean.writeLog("CustomSearchQXKZ——sqlCondition:" + sqlCondition);
            return sqlCondition;
        } else {

            int count = 0;

            //遍历  区域管理员表
            rs.execute("select * from " + QYGLYtablename);
            while (rs.next()) {
                if (user.getUID() == Util.getIntValue(rs.getString("ry"))) {
                    String QYBM = Util.null2String(rs.getString("QYBM"));
                    sqlCondition += " or t1.QYBM = '" + QYBM + "' ";
                    count++;
                }
            }
            if (count > 0) {
                sqlCondition += ") ";
                baseBean.writeLog("CustomSearchQXKZ——sqlCondition:" + sqlCondition);
                return sqlCondition;
            }

            //遍历  数据上报员表
            rs.execute("select * from " + SJSBYtablename);
            while (rs.next()) {
                if (user.getUID() == Util.getIntValue(rs.getString("ry"))) {
                    String WERKS = Util.null2String(rs.getString("WERKS"));//人事范围编码
                    String BMBM = Util.null2String(rs.getString("BMBM"));//部门编码
                    if ("ZB01".equals(WERKS) || "8004".equals(WERKS) || "8007".equals(WERKS)) {
                        sqlCondition += " or (t1.WERKS = '" + WERKS + "' and t1.BMBM = '" + BMBM + "') ";
                    } else {
                        sqlCondition += " or (t1.WERKS = '" + WERKS + "') ";
                    }
                    count++;
                }
            }
            if (count > 0) {
                sqlCondition += ") ";
                baseBean.writeLog("CustomSearchQXKZ——sqlCondition:" + sqlCondition);
                return sqlCondition;
            }

            String[] st = STtablename.split(",");
            for (String tablename : st) {
                //遍历  视图
                rs.execute("select * from " + tablename);
                while (rs.next()) {
                    String id = Util.null2String(rs.getString("id"));//人员id
                    if (user.getUID() == Util.getIntValue(id)) {
                        //获取人事范围编码
                        recordSet.execute("select * from " + HrmFieldtablename);
                        recordSet.next();
                        String rsfw = Util.null2String(recordSet.getString("rsfw"));//人事范围字段名
                        recordSet.execute("select " + rsfw + " from " + RLZYtablename + " where id='" + id + "'");
                        recordSet.next();
                        String WERKS = Util.null2String(recordSet.getString(rsfw));//人事范围编码
                        //获取部门编码
                        recordSet.execute("select departmentcode from hrmresource hr left join hrmdepartment hd on hr" +
                                ".departmentid = hd.id where hr.id='" + id + "'");
                        recordSet.next();
                        String BMBM = Util.null2String(recordSet.getString("departmentcode"));//部门编码

                        if ("ZB01".equals(WERKS) || "8004".equals(WERKS) || "8007".equals(WERKS)) {
                            sqlCondition += " or (t1.WERKS = '" + WERKS + "' and t1.BMBM = '" + BMBM + "') ";
                        } else {
                            sqlCondition += " or (t1.WERKS = '" + WERKS + "') ";
                        }
                    }
                }
            }
        }
        sqlCondition += ") ";
        baseBean.writeLog("CustomSearchQXKZ——sqlCondition:" + sqlCondition);
        return sqlCondition;
    }

}
